#include <REGX52.H>
 
typedef unsigned int u16; //??????????????
typedef unsigned char u8;

#define KEY_MATRIX_PORT P3 //????????????
#define SMG_A_DP_PORT P0 //???????????
 
//????????0~9 N F ?????
 
u8 nixie[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x37,0x71};
 
unsigned int Code = 0;//?????????
unsigned int TempCode = 0;//???????????
 
unsigned int NewCode = 0;//??????????
 
unsigned int LNew=0,New=0;//?????????????
 
unsigned int Newlate=0;//?????????????
 
void wei(unsigned char pizz);//???????????????
 
unsigned int key=0;//?????????
 
unsigned int T=0;//????????
 
unsigned int TLL=0;//????????
 
unsigned int F=0;//????????
 
static unsigned char i = 0;//?????
 
void delay_10us(u16 ten_us)//????
 
{
 
while(ten_us--);
 
}
 
u8 key_matrix_ranks_scan(void)
 
{
 
u8 key_value=0;
 
 
 
KEY_MATRIX_PORT=0xf7;//??????0,????1
 
if(KEY_MATRIX_PORT!=0xf7)//???????????
 
{
 
delay_10us(500);//??
 
switch(KEY_MATRIX_PORT)//?????????????
 
{
 
case 0x77: key_value=1;break;
 
case 0xb7: key_value=5;break;
 
case 0xd7: key_value=9;break;
 
case 0xe7: key_value=13;break;
 
}
 
}
 
while(KEY_MATRIX_PORT!=0xf7);//??????
 
 
 
KEY_MATRIX_PORT=0xfb;//??????0,????1
 
if(KEY_MATRIX_PORT!=0xfb)//???????????
 
{
 
delay_10us(500);//??
 
switch(KEY_MATRIX_PORT)//?????????????
 
{
 
case 0x7b: key_value=2;break;
case 0xbb: key_value=6;break;
case 0xdb: key_value=10;break;
case 0xeb: key_value=14;break;
}
 
}
 
while(KEY_MATRIX_PORT!=0xfb);//??????
 
 
 
KEY_MATRIX_PORT=0xfd;//??????0,????1
 
if(KEY_MATRIX_PORT!=0xfd)//???????????
 
{
 
delay_10us(500);//??
 
switch(KEY_MATRIX_PORT)//?????????????
 
{
 
case 0x7d: key_value=3;break;
case 0xbd: key_value=7;break;
case 0xdd: key_value=11;break;
case 0xed: key_value=15;break;
} 
}
 
while(KEY_MATRIX_PORT!=0xfd);//??????
 
 
 
KEY_MATRIX_PORT=0xfe;//??????0,????1
 
if(KEY_MATRIX_PORT!=0xfe)//???????????
 
{
 
delay_10us(500);//??
 
switch(KEY_MATRIX_PORT)//?????????????
 
{
 
case 0x7e: key_value=4;break;
case 0xbe: key_value=8;break;
case 0xde: key_value=12;break;
case 0xee: key_value=16;break;
}
 
}
 
while(KEY_MATRIX_PORT!=0xfe);//??????
 
 
 
return key_value;
 
}
 
u8 key_matrix_flip_scan(void)
 
{
 
static u8 key_value=0;
 
 
 
KEY_MATRIX_PORT=0x0f;//??????0,???1
 
if(KEY_MATRIX_PORT!=0x0f)//????????
 
{
 
delay_10us(500);//??
 
if(KEY_MATRIX_PORT!=0x0f)
 
{
 
KEY_MATRIX_PORT=0x0f;//???
 
switch(KEY_MATRIX_PORT)//????0,????????
 
{
 
case 0x07: key_value=1;break;
case 0x0b: key_value=2;break;
case 0x0d: key_value=3;break;
case 0x0e: key_value=4;break;
 
}
 
KEY_MATRIX_PORT=0xf0;//???
 
switch(KEY_MATRIX_PORT)//????0,????????
 
{
 
case 0x70: key_value=key_value;break;
case 0xb0: key_value=key_value+4;break;
case 0xd0: key_value=key_value+8;break;
case 0xe0: key_value=key_value+12;break; 
}
 
while(KEY_MATRIX_PORT!=0xf0);//??????
 
}
 
}
 
else
 
key_value=0;
 
return key_value;
 
}
 
void smg_display(unsigned char pizz)
 
{
 
    switch(pizz)//??
 
{
 
case 1: P2=0x7f;break;//?????
case 2: P2=0xbf;break;//?????
case 3: P2=0xdf;break;//?????
case 4: P2=0xef;break;//?????
case 5: P2=0xf7;break;//?????
case 6: P2=0xfb;break;//?????
case 7: P2=0xfd;break;//?????
case 8: P2=0xfe;break;//?????
}
 
}
 
void main()
 
{
 
  unsigned char k;//??????
 
while(1)
 
{
 
  key=key_matrix_ranks_scan();
 
  if(key!=0)
 
 {
 
         if(key<=10)
 
         {
 
           if(k<4)
 
            {
 
 if(T==0)
 
 {
 
               Code*=10;            
 
               Code+=key%10;
 
  if(TLL!=1)
 
{
 
                    k++;
 
}
 
 }
 
if(TLL==1)
 
{
 
    TempCode*=10;            
 
             TempCode+=key%10;
 
Code=TempCode;
 
             k++;
 
     LNew=1;
 
T=0;
 
}
 
         }    
 
else
 
{
 
 k=0;
 
  if(Code==8888&&Newlate==0)
 
  {
 
T=1;
 
Code=8888;
 
TLL=1;
 
   }
 
  else if(Code==NewCode&&Newlate==1)
 
  {
 
   T=1;
 
   LNew=0;
 
   TLL=0;
 
  }
 
       else
 
 {
 
  F=1;
 
 }
 
  }
 
            }
 
            else if(key==16)
 
            {
 
                k=0;
 
                Code=0;
 
T=0;
 
  F=0;
 
  LNew=0;
 
  New=0;
 
            }
 
            else if(key==15)
 
            {
 
               if(LNew==1)
 
  {
 
 NewCode=TempCode;
 
Newlate=1;
 
 TLL=0;
 
 Code=0;
 
 k=0;
 
 }
 
            }
 
        }
 
 
 
if(F==0&&T==0&&Code!=0)
 
    {
 
   for(i=0;i<8;i++)
 
      {
 
   switch(i)
 
        {  
 
        case 0:smg_display(1);P0=~nixie[Code/1000%10];break;
        case 1:P0=0xff;break;
        case 2:smg_display(3);P0=~nixie[Code/10%10];break;
        case 3:P0=0xff;break;
        case 4:smg_display(2);P0=~nixie[Code/100%10];break;
        case 5:P0=0xff;break;
        case 6:smg_display(4);P0=~nixie[Code%10];break;
        case 7:P0=0xff;break;
 
        }
 
delay_10us(50);
 
   }
 
}
 
 if(T==1&&LNew==0)
 
 {
 
 for(i=0;i<5;i++)
 
     {
 
    switch(i)
 
        {  
 
case 0:P0=0xff;break;
 
        case 1:smg_display(7);P0=~nixie[0];break;
 
        case 2:P0=0xff;break;
 
case 3:smg_display(8);P0=~nixie[10];break;
 
        case 4:P0=0xff;break;
 
        }
 
delay_10us(50);
 
}
 
}
 
 if(F==1)
 
 {
 
 for(i=0;i<7;i++)
 
     {
 
   switch(i)
 
       {  
 
case 0:P0=0xff;break;
 
        case 1:smg_display(6);P0=~nixie[0];break;
 
        case 2:P0=0xff;break;
 
case 3:smg_display(7);P0=~nixie[11];break;
 
        case 4:P0=0xff;break;
 
case 5:smg_display(8);P0=~nixie[11];break;
 
        case 6:P0=0xff;break;
 
       }
 
delay_10us(50);
 
 }
 
 }
 
 if(Code==0)
 
{
 
  P0=0x00;  //?????  ???????
 
}
 
    
 
}
 
}